/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Thing;

import Core.Status;
import Core.Way;
import Core.Vector2;
import java.util.Vector;
import javax.microedition.lcdui.game.Sprite;
/**
 *
 * @author THINH
 */
public abstract class Character extends Thing{
    //--------------------field-------------------------------
    protected Vector gifts = new Vector(0);
    private int index = 0;
    private static final int numItem = 3;
    public Flying flying = null;
    protected float power = 0;
    public final static int MAX_SPEED = 9;
    //shoot
    private boolean shooting = false;
    private long shootTime = 0;
    //sprite
    int[] standSeq, goRightSeq, jumpSeq, shootSeq, shootNormalSeq, sitSeq;
    //basic attribute of character
    protected float strength = 0;
    public int speed = 6;
    
    public int MAX_HEALTH = 100;
    public int currenthealth = MAX_HEALTH;
    public int damagebase = 47;
    public int damagebonus = 0;
    public int armor = 4;
    //constructor
    public Character(){
        super();
    }
    public Character(String pathImage, int frameWidth, int frameHeight, int[]standSequence, 
            int[]goRightSequence, int[] jumpSequence, int[] shootSequence, int[] shootNormalSeq, int[] sitSequence){
        super(pathImage, frameWidth, frameHeight, standSequence);
        standSeq = standSequence;
        goRightSeq = goRightSequence;
        jumpSeq = jumpSequence;       
        shootSeq = shootSequence;
        this.shootNormalSeq = shootNormalSeq;
        sitSeq = sitSequence;
        status.value = status.StandRight;
    }
    //main func
    public void update(){
        //code here
        
        if(status.isJump() ){
            if(vel.y>0){
                sprite.setFrame(1);
            }
            if(vel.y<0){
                sprite.setFrame(0);
            }
            if(vel.y==0){
                sprite.setFrame(3);
            }
        }
        else{
            vel.goToZero();
        }
        if(this.getVelocity().y>15){
            
            this.setVelocity(new Vector2(vel.x,15));
        }
        if(flying!=null){
            flying.update();
            if(System.currentTimeMillis() - shootTime>600){
                flying.isLive = false;
            }
        }
        if(shooting){
            int frame = sprite.getFrame();
            if(frame==10){                
                shooting=false;
                stand();
            }            
        }
        
        if(currenthealth>MAX_HEALTH){
            currenthealth = MAX_HEALTH;
        }
        super.update();        
    }
    
    public void move(int way){
        if(vel.getLength()<Character.MAX_SPEED){
            switch(way){
                case Way.LEFT:
                    moveLeft();                    
                    break;
                case Way.RIGHT:
                    moveRight();
                    break;
                case Way.UP:
                    jump();
                    break;
            }
        }
    }
    public void stand(){
        if((!status.isStand() && !shooting) || (!status.isShoot()&&!status.isStand())){
            if(status.isLeft()){
                sprite.setFrameSequence(standSeq);
                int x = sprite.getX();
                int y = sprite.getY();
                sprite.setTransform(Sprite.TRANS_MIRROR);
                sprite.setPosition(x, y);
                status.value = Status.StandLeft;
            }
            if(status.isRight()){
                sprite.setFrameSequence(standSeq);                
                int x = sprite.getX();
                int y = sprite.getY();
                sprite.setTransform(Sprite.TRANS_NONE);
                sprite.setPosition(x, y);
                status.value = Status.StandRight;
            }
            
        }
    }
    public void sit(){
        if(status.value != status.Sit){
            if(status.isLeft()){
                sprite.setFrameSequence(sitSeq);
                int x = sprite.getX();
                int y = sprite.getY();
                sprite.setTransform(Sprite.TRANS_MIRROR);
                sprite.setPosition(x, y);
                status.value = Status.SitLeft;
            }
            if(status.isRight()){
                sprite.setFrameSequence(sitSeq);                
                int x = sprite.getX();
                int y = sprite.getY();
                sprite.setTransform(Sprite.TRANS_NONE);
                sprite.setPosition(x, y);
                status.value = Status.SitRight;
            }
        }
    }
    private void moveLeft(){
        if(status.value != status.GoLeft && !status.isJump()){
            sprite.setFrameSequence(goRightSeq);
            int x = sprite.getX();
            int y = sprite.getY();
            sprite.setTransform(Sprite.TRANS_MIRROR);
            sprite.setPosition(x, y);
            status.value = status.GoLeft;
        }
        if(!status.isJump()){
            vel.set(new Vector2(-1*speed,vel.y));
        }
    }
    private void moveRight(){
        if(status.value!= status.GoRight && !status.isJump()){
            sprite.setFrameSequence(goRightSeq);
            int x = sprite.getX();
            int y = sprite.getY();
            sprite.setTransform(Sprite.TRANS_NONE);
            sprite.setPosition(x, y);
            status.value = status.GoRight;
        }
        if(!status.isJump()){
            vel.set(new Vector2(1*speed,vel.y));        
        }
    }
    
    private void shoot(){
        
            sprite.setFrameSequence(shootNormalSeq);
            if(status.isLeft()){
                status.value = status.ShootLeft;
            }
            else if(status.isRight()){
                status.value = status.ShootRight;
            }
            shooting = true;
            shootTime = System.currentTimeMillis();
    }
    private void jump(){
        if(!status.isJump() && !status.isShoot()){
            if(status.isLeft()){
                sprite.setFrameSequence(jumpSeq);
                int x = sprite.getX();
                int y = sprite.getY();
                sprite.setTransform(Sprite.TRANS_MIRROR);
                sprite.setPosition(x-6, y);          
                status.value = status.JumpLeft;
            }
            if(status.isRight()){
                sprite.setFrameSequence(jumpSeq);
                int x = sprite.getX();
                int y = sprite.getY();
                sprite.setTransform(Sprite.TRANS_NONE);
                sprite.setPosition(x+6, y);
                status.value = status.JumpRight;
            } 
                
            vel.set(new Vector2(vel.x,-15));
        }
    }
    
    //func
    public void activeGift(int index){
        if(gifts == null )
            return;
        if(index>=0 && index<gifts.size()){
            Gift g = (Gift)gifts.elementAt(index);
            if(g !=null){
                g.Active();
                if(g.TypeGift() == 0)
                    gifts.removeElementAt(index);
            }
        }
    }
    
    public float seePower(){
        return power;
    }
    
    public void shoot(Vector2 direction){
        if(flying!=null){
            flying.shoot(direction);
            flying.setPos(this.getSprite().getX(), this.getSprite().getY());
            this.shoot();
            //flying.status.value = Status.ShootNormal;
        }
    }
    
    //add code here
   
   
   
   public void addGift(Gift g)
   {
       if(gifts.size() == numItem)
       {
           if(index > 2)
               index = 0;
           gifts.setElementAt(g, index);
           index++;
       }
       else
           gifts.addElement(g);
       if(g.TypeGift() == 1)
       {
           activeGift(index);
       }
   }
   
   public Vector getGifts()
   {
       return gifts;
   }
}
